package com.chima.dao;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.chima.domain.DetailPost;
public class DetailPostDao extends HibernateDaoSupport{
	//分页查询
	public List<DetailPost> findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
		return this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
	}
   
	//查找总页数
public int findByTotalCount(int emp) {
		
		long temp=2312312l;
		if(emp==0){
			List list= this.getHibernateTemplate().find("select count(*) from DetailPost");
			 temp = (Long) list.get(0);
		}else{
			List list= this.getHibernateTemplate().find("select count(*) from DetailPost d where d.employee.id='"+emp+"' ");
			 temp = (Long) list.get(0);
		}
		return (int) temp;
	}
	public void save(DetailPost detailPost) {
		Session session   =this.getSession();
//		this.getHibernateTemplate().save(detailPost);
		this.getSession().save(detailPost);
		super.releaseSession(session);
	}
	public void update(DetailPost detailPost) {
		this.getHibernateTemplate().update(detailPost);
	}
	
	public void delete(int id) {
		this.getHibernateTemplate().delete(this.getHibernateTemplate().load(DetailPost.class, id));
	}
	public List<DetailPost>findByPostAndEmp(int post,int emp) {
		Session session   =this.getSession();
		List<DetailPost> p= this.getSession().createQuery(" from DetailPost where  post.id=? and employee.id=?").setParameter(0, post).setLong(1, emp).list();
		super.releaseSession(session);
		return p;
	}
	public DetailPost findByNameAndEmp(String name, int emp) {
		Session session   =this.getSession();
		DetailPost p=(DetailPost) this.getSession().createQuery(" from DetailPost where  name=?  and employee.id=?").setParameter(0, name).setParameter(1, emp).uniqueResult();
		super.releaseSession(session);
		return p;
	}
	
	public List<DetailPost>findByDeptId(int DeptId) {
		Session session   =this.getSession();
		List<DetailPost> p= this.getSession().createQuery(" from DetailPost where department.id=?").setParameter(0, DeptId).list();
		super.releaseSession(session);
		return p;
	}
	public void deleteDetailPostByEmp(int empid) {
		
//		String hql="DELETE * from DetailPost where employee.id==?";
//		Session session=this.getSession();
//		session.createQuery(hql).setParameter(0,empid ).executeUpdate();
//		super.releaseSession(session);
		 Session session=this.getSession();
		 String sql1=" delete  from t_postdetail where empID=?";
		 session.createSQLQuery(sql1).setParameter(0, empid).executeUpdate();
		 super.releaseSession(session);
		
	}
}
